﻿Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.IO.Compression
Imports System.Workflow.ComponentModel
Imports System.Workflow.Runtime

Namespace System.Workflow.Runtime.Hosting
    Public MustInherit Class WorkflowPersistenceService
        Inherits WorkflowRuntimeService
        ' Methods
        Protected Sub New()
        End Sub

        Protected Shared Function GetDefaultSerializedForm(ByVal activity As Activity) As Byte()
            Dim buffer As Byte()
            Dim now As DateTime = DateTime.Now
            [Using] stream As MemoryStream = New MemoryStream(&H2800)
                stream.Position = 0
                activity.Save(stream)
                [Using] stream2 As MemoryStream = New MemoryStream(CInt(stream.Length))
                    [Using] stream3 As GZipStream = New GZipStream(stream2, CompressionMode.Compress, True)
                        stream3.Write(stream.GetBuffer, 0, CInt(stream.Length))
                    End [Using]
                    Dim info As ActivityExecutionContextInfo = DirectCast(activity.GetValue(Activity.ActivityExecutionContextInfoProperty), ActivityExecutionContextInfo)
                    Dim span As TimeSpan = DirectCast((DateTime.Now - now), TimeSpan)
                    WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, "Serialized a {0} with id {1} to length {2}. Took {3}.", New Object() { info, info.ContextGuid, stream2.Length, span })
                    buffer = stream2.GetBuffer
                    Array.Resize([Of] Byte)((buffer), Convert.ToInt32(stream2.Length))
                End [Using]
            End [Using]
            Return buffer
        End Function

        Protected Friend Shared Function GetIsBlocked(ByVal rootActivity As Activity) As Boolean
            Return CBool(rootActivity.GetValue(WorkflowExecutor.IsBlockedProperty))
        End Function

        Protected Friend Shared Function GetSuspendOrTerminateInfo(ByVal rootActivity As Activity) As String
            Return CStr(rootActivity.GetValue(WorkflowExecutor.SuspendOrTerminateInfoProperty))
        End Function

        Protected Friend Shared Function GetWorkflowStatus(ByVal rootActivity As Activity) As WorkflowStatus
            Return DirectCast(rootActivity.GetValue(WorkflowExecutor.WorkflowStatusProperty), WorkflowStatus)
        End Function

        Protected Friend MustOverride Function LoadCompletedContextActivity(ByVal scopeId As Guid, ByVal outerActivity As Activity) As Activity

        Protected Friend MustOverride Function LoadWorkflowInstanceState(ByVal instanceId As Guid) As Activity

        Protected Shared Function RestoreFromDefaultSerializedForm(ByVal activityBytes As Byte(), ByVal outerActivity As Activity) As Activity
            Dim activity As Activity
            Dim now As DateTime = DateTime.Now
            Dim stream As New MemoryStream(activityBytes) { _
                .Position = 0 _
            }
            [Using] stream2 As GZipStream = New GZipStream(stream, CompressionMode.Decompress, True)
                activity = Activity.Load(stream2, outerActivity)
            End [Using]
            Dim span As TimeSpan = DirectCast((DateTime.Now - now), TimeSpan)
            WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, "Deserialized a {0} to length {1}. Took {2}.", New Object() { activity, stream.Length, span })
            Return activity
        End Function

        Protected Friend MustOverride Sub SaveCompletedContextActivity(ByVal activity As Activity)

        Protected Friend MustOverride Sub SaveWorkflowInstanceState(ByVal rootActivity As Activity, ByVal unlock As Boolean)

        Protected Friend MustOverride Function UnloadOnIdle(ByVal activity As Activity) As Boolean

        Protected Friend MustOverride Sub UnlockWorkflowInstanceState(ByVal rootActivity As Activity)

    End Class
End Namespace

